package com.yanqu.road.server.cross3server.cmd.server;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross3.server.CrossChannelRouteProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.protocol.Cross3Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.List;

@Cmd(code = Cross3Protocol.C3_ROUTE_BATCH_GAME_MSG, desc = "批量指定游戏服分发")
public class RouteBatchGameMsgCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossChannelRouteProto.RouteBatchGameMsg reqMsg = CrossChannelRouteProto.RouteBatchGameMsg.parseFrom(packet.getBytes());

        // 拿一下真实的协议号
        int gameCode = reqMsg.getGameCode();
        List<CrossChannelRouteProto.BatchGameTemp> gameServerList = reqMsg.getGameServerList();

        // 用真实的协议号转发到游戏服
        if (gameServerList.isEmpty()) {
            byte[] msg = reqMsg.getMsg().toByteArray();
            YanQuMessage message = YanQuMessageUtils.buildMessageByte(gameCode, msg);
            MessageHelper.sendAllServer(message); // 全部游戏服
        } else {
            for (CrossChannelRouteProto.BatchGameTemp batchGameTemp : gameServerList) {
                long gameServerId = batchGameTemp.getGameServerId();
                List<Long> userIdList = batchGameTemp.getUserIdList();
                MessageHelper.sendPacketToAll(gameCode, reqMsg.getMsg(), gameServerId, userIdList, false);
            }
        }

    }

    @Override
    public void sendData() {

    }
}
